package com.lzxuni.modules.cropinformation.controller;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.annotation.SysLog;
import com.lzxuni.common.constant.LogConstant;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.cropinformation.entity.TblCropInformation;
import com.lzxuni.modules.cropinformation.entity.TblGrowthInformation;
import com.lzxuni.modules.cropinformation.service.TblCropInformationService;
import com.lzxuni.modules.cropinformation.service.TblGrowthInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 作物生长信息 前端控制器
 * </p>
 *
 * @author rdf
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/LR_TblGrowthInformation/TblGrowthInformation")
public class TblGrowthInformationController extends BaseController {

    @Autowired
    private TblCropInformationService tblCropInformationService;
    @Autowired
    private TblGrowthInformationService tblGrowthInformationService;

    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/growthinformation/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,String queryJson,String cropName,String cropModel){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        TblCropInformation cropInformation = new TblCropInformation();
        if(StringUtils.isNotEmpty(cropName)){
            cropInformation.setCropName(cropName);
        }
        if(StringUtils.isNotEmpty(cropModel)){
            cropInformation.setCropModel(cropModel);
        }
        PageData pageData = getPageData(tblCropInformationService.queryPage(pageParameter, cropInformation));
        return R.ok().put("data", pageData);
    }

    //生长信息列表页面
    @RequestMapping("/IndexSzxx")
    public ModelAndView IndexSzxx(String cropId,String cropName,String cropModel) {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/growthinformation/indexszxx");
        mv.addObject("cropId", cropId);
        mv.addObject("cropName", cropName);
        mv.addObject("cropModel", cropModel);
        return mv;
    }

    //查询数据
    @RequestMapping("/GetListszxx")
    public Object GetListszxx(String pagination,String queryJson,String growthName,String cropModel,String cropId){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        TblGrowthInformation tblGrowthInformation = new TblGrowthInformation();
        if(StringUtils.isNotEmpty(growthName)){
            tblGrowthInformation.setGrowthName(growthName);
        }
//        if(StringUtils.isNotEmpty(cropModel)){
//            cropInformation.setCropModel(cropModel);
//        }
        tblGrowthInformation.setCropId(cropId);
        PageData pageData = getPageData(tblGrowthInformationService.queryPageszxx(pageParameter, tblGrowthInformation));
        return R.ok().put("data", pageData);
    }

    //新增跳页
    @RequestMapping("/Form")
    public ModelAndView Forma(String cropId,String cropName,String cropModel) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/growthinformation/form");
        mv.addObject("cropId", cropId);
        mv.addObject("cropName", cropName);
        mv.addObject("cropModel", cropModel);
        return mv;
    }

    //保存
    @SysLog(categoryId = LogConstant.OPERATEID, module = "作物生长信息", operateType = LogConstant.INSERT)
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue,TblGrowthInformation tblGrowthInformation) {
        if(StringUtils.isNotEmpty(keyValue)){
            tblGrowthInformation.setGrowthId(keyValue);
            tblGrowthInformation.setCropId(tblGrowthInformation.getCropId());
            tblGrowthInformation.setLowerMonth(tblGrowthInformation.getLowerMonth());
            tblGrowthInformation.setUpperMonth(tblGrowthInformation.getUpperMonth());
            tblGrowthInformation.setLowerTemperature(tblGrowthInformation.getLowerTemperature());
            tblGrowthInformation.setUpperTemperature(tblGrowthInformation.getUpperTemperature());
            tblGrowthInformation.setGrowthName(tblGrowthInformation.getGrowthName());
            tblGrowthInformation.setGrowthFertilizer(tblGrowthInformation.getGrowthFertilizer());
            tblGrowthInformation.setGrowthPests(tblGrowthInformation.getGrowthPests());
            tblGrowthInformation.setGrowthCareful(tblGrowthInformation.getGrowthCareful());
            tblGrowthInformation.setGrowthRemarks(tblGrowthInformation.getGrowthRemarks());
            tblGrowthInformationService.updateById(tblGrowthInformation);
            return R.ok().put("data", "修改成功！");
        }else {
            String grId = UuidUtil.get32UUID();
            tblGrowthInformation.setGrowthId(grId);
            tblGrowthInformation.setCropId(tblGrowthInformation.getCropId());
            tblGrowthInformation.setCropNamee(tblGrowthInformation.getCropName());
            tblGrowthInformation.setCropModell(tblGrowthInformation.getCropModel());
            tblGrowthInformation.setCreateTime(new Date());
            tblGrowthInformation.setLowerMonth(tblGrowthInformation.getLowerMonth());
            tblGrowthInformation.setUpperMonth(tblGrowthInformation.getUpperMonth());
            tblGrowthInformation.setLowerTemperature(tblGrowthInformation.getLowerTemperature());
            tblGrowthInformation.setUpperTemperature(tblGrowthInformation.getUpperTemperature());
            tblGrowthInformation.setGrowthName(tblGrowthInformation.getGrowthName());
            tblGrowthInformation.setGrowthFertilizer(tblGrowthInformation.getGrowthFertilizer());
            tblGrowthInformation.setGrowthPests(tblGrowthInformation.getGrowthPests());
            tblGrowthInformation.setGrowthCareful(tblGrowthInformation.getGrowthCareful());
            tblGrowthInformation.setGrowthRemarks(tblGrowthInformation.getGrowthRemarks());
            tblGrowthInformationService.save(tblGrowthInformation);
            return R.ok().put("data", "保存成功！");
        }
    }

    //删除
    @SysLog(categoryId = LogConstant.OPERATEID,module = "作物信息",operateType = LogConstant.DELETE)
    @RequestMapping("/DeleteForm")
    public Object delete(String growthId) {
        tblGrowthInformationService.removeById(growthId);
        return R.ok("删除成功");
    }
    //查看
    @RequestMapping("/See")
    public ModelAndView See(String keyValue,String cropId,String cropName,String cropModel) throws Exception {
        ModelAndView mv = new ModelAndView("/LR_TblCropInformation/growthinformation/see");
        mv.addObject("cropId", cropId);
        mv.addObject("cropName", cropName);
        mv.addObject("cropModel", cropModel);
        return mv;
    }



    //作物名称
    @RequestMapping("/GetTree")
    public Object getTree(String cropModel) {
        List<Tree> comptList = tblGrowthInformationService.getTree(cropModel);
        return R.ok().put("data", comptList);
    }

}

